home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C12 / Strings2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  665 b   |  29 lines

  1. //: C12:Strings2.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // With auto type conversion
  7. #include "../require.h"
  8. #include <cstring>
  9. #include <cstdlib>
  10. using namespace std;
  11.  
  12. class Stringc {
  13.   char* s;
  14. public:
  15.   Stringc(const char* S = "") {
  16.     s = (char*)malloc(strlen(S) + 1);
  17.     require(s != 0);
  18.     strcpy(s, S);
  19.   }
  20.   ~Stringc() { free(s); }
  21.   operator const char*() const { return s; }
  22. };
  23.  
  24. int main() {
  25.   Stringc s1("hello"), s2("there");
  26.   strcmp(s1, s2); // Standard C function
  27.   strspn(s1, s2); // Any string function!
  28. } ///:~
  29.